classdef SetupWidget
    %GRIDWIDGET Handles the setup data needed for SWAN runs
    %   This widget (class) draws a gridpanel wich handles all the
    %   start-up, initial, physical and numerical options needed for SWAN
    %   runs. These settings are standard for each wave propagation in the
    %   project. Also, two tables are populated for run cases and output
    %   settings. 
    
    % This file is part of CISNE.
%
%     CISNE is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     any later version.
%
%     CISNE is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
%
%     You should have received a copy of the GNU General Public License
%     along with CISNE.  If not, see <http://www.gnu.org/licenses/>.

% Copyright 2011. Jose Maria Garcia-Valdecasas Bernal
    
    properties (SetAccess=private)
        m_gridmodel=[];
        m_gridList=[];
        m_casePanel=[];
        m_outPanel=[];
        m_Figure=[]; %parent figure (CISNE main window).
        m_jstr=java.lang.Class.forName('java.lang.String',true,...
            java.lang.Thread.currentThread().getContextClassLoader());
        m_jnum=java.lang.Class.forName('java.lang.Float', true,...
            java.lang.Thread.currentThread().getContextClassLoader());
        m_jint=java.lang.Class.forName('java.lang.Integer',true,...
            java.lang.Thread.currentThread().getContextClassLoader());
        m_jbool=java.lang.Class.forName('java.lang.Boolean', true,...
            java.lang.Thread.currentThread().getContextClassLoader());
    end
    
    methods
        function obj=SetupWidget(parent)
            %constructor function
            handles=guidata(parent);
            obj.m_Figure=handles.figure1;
            clear handles; %remove from memory.
            
            %get parent size to fit widget inside parent
            aux=get(parent,'Position');
            width=aux(3)-4; % 2pixels margin
            height=aux(4)-4;
            
            % create property grid
            obj.m_gridList = java.util.ArrayList();
            obj.m_gridmodel = com.jidesoft.grid.PropertyTableModel(obj.m_gridList);
            obj.m_gridmodel.expandAll();
            grid = com.jidesoft.grid.PropertyTable(obj.m_gridmodel);
            pane = com.jidesoft.grid.PropertyPane(grid);
            javacomponent(pane, [2 2 width height],parent);

            hModel = handle(obj.m_gridmodel, 'CallbackProperties');
            set(hModel, 'PropertyChangeCallback', @obj.onPropertyChangeFcn);
            
            %initialize grid case and output panels.
            obj.initGridPanel();
            %obj.initCasePanel();
            %obj.initOutPanel();            
        end
        
        %functions
        initGridPanel(obj);
        initCasePanel(obj);
        initOutPanel(obj);
     
        function value=getValue(obj,item)
            if verLessThan('matlab', '7.9.0') %previous to 2009b
                %can't use the '.' format, split it
                aux=regexp(item,'\.','split');
                if length(aux)==2
                    aux=aux{2};
                else
                    aux=aux{1};
                end
                for j=0:size(obj.m_gridList)-1
                    prop=obj.m_gridList.get(j);
                    if strcmp(prop.getName(),aux)
                        value=prop.getValue();
                        break
                    end
                end
                    
            else
                value=obj.m_gridmodel.getProperty(item).getValue();
            end
        end
    end
    
    methods (Static)
        data=loadData();
    end
end

